<?php
namespace app\common\controller;

use think\auth\Auth;
use think\Loader;
use think\Controller;
use think\Db;

/**
* 商家后台公用基础控制器
* @author Kaisa
* @version 2017-02-09
*/
class StoreBaseController extends Controller {
	
    protected function _initialize() {
        parent::_initialize();
		
        $store_id = session('storeInfo')['id'];
        if (empty($store_id)) {
            $this->redirect('web/login/index');
        }
		
		// 当前路径URL，菜单用到
		$this->assign('current_url', Loader::parseName(request()->controller().'/'.request()->action()));		
		$store_top_menus=cache("store_top_menus");//菜单缓存
		
		$employee = session('employee');//员工信息
		$storeinfo  = session('storeInfo');
		$type = session('login_type');//登录类型
		$chainname = "";//连锁店名称
		$employeeinfo = Db::table('long_line_employee')->where('id',$employee['id'])->find();
		if($employee&&$type==2){			
			$zhiwei = Db::table('long_line_zhiwei')->field('rules')
			->where('agent_id',$employeeinfo['agent_id'])
			->where('id',$employeeinfo['z_id'])->find();
			if($zhiwei){			
				$where['id'] = array('in',$zhiwei['rules']);
			}else{
				$where['id'] = 203;
			}	
			
			//连锁店名称 连锁店菜单
			$chainstore = Db::table('long_mall_chainstore')->where('agent_id',session('storeInfo')['id'])->find($employee['chainstore_id']);
			$chainname = $chainstore['name'];
			if($chainstore){
				$role = Db::table('long_agent_role')->field('rules')->where('id',3)->find();
				$where['id'] = array('in',$role['rules']);
				$where['pid'] = 0;
				$store_top_menus=db("store_menu")->where($where)->order("sort asc ")->select();
			}
		}
		
		if(!$store_top_menus){
			$role = Db::table('long_agent_role')->field('rules')->where('id',$storeinfo['agent_type'])->find();
			$where['id'] = array('in',$role['rules']);
			$where['pid'] = 0;
			$store_top_menus=db("store_menu")->where($where)->order("sort asc ")->select();
//			cache("store_top_menus",$store_top_menus);
		}
		
		$this->assign("chainsotrename",$chainname);
		$this->assign("store_top_menus",$store_top_menus);
        $this->getMenu();
		$this->check_act();
    }
/**
	 * 判断是否显示后台左边菜单
	 */
	public function check_act(){
		$request = \think\Request::instance();
		$controller=strtolower($request->controller());
		$array_controller=array("index");
		$action=$request->action();
		$is_show_left=0;
		if(!in_array($controller, $array_controller)){
			$is_show_left=1;
		}
		$this->assign("is_show_left",$is_show_left);
	}
  
    /**
     * 获取侧边栏菜单
     */
    protected function getMenu() {
        $menu     = [];
        $storeinfo  = session('storeInfo');
        $auth     = new Auth();
		$where['status'] = 1;
		$employee = session('employee');//员工信息
		$type = session('login_type');//登录类型
		$employeeinfo = Db::table('long_line_employee')->where('id',$employee['id'])->find();
		if($employee&&$type==2){
			$zhiwei = Db::table('long_line_zhiwei')->field('rules')
			->where('agent_id',$employeeinfo['agent_id'])
			->where('id',$employeeinfo['z_id'])->find();	
			if($zhiwei){			
				$where['id'] = array('in',$zhiwei['rules']);
			}		
		}
		if(input("top_menu_id")){
			$where['pid']=input("top_menu_id");
			session("top_menu_id",input("top_menu_id"));
			$this->assign('top_menu_id',input('top_menu_id'));
		}else{
			if(session("top_menu_id")){
				$where['pid']=session("top_menu_id");	
				$this->assign('top_menu_id',session("top_menu_id"));
			}else{
				$where['pid']=203;
				$this->assign('top_menu_id',"");
			}
		}
		
		$role = Db::table('long_agent_role')->field('rules')->where('id',$storeinfo['agent_type'])->find();
		$where['id'] = array('in',$role['rules']);
        $auth_rule_list = db('store_menu')->where($where)->order(['sort' => 'asc', 'id' => 'asc'])->select();
//	    foreach ($auth_rule_list as $value) {
//			$menu[] = $value;
//      }
//      $menu = !empty($menu) ? array2tree($menu) : [];
        $this->assign('menu', $auth_rule_list);
    }
}
